const app = getApp()
import util from "../../../utils/words";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLike: false,
    isCollection: false,
    text: '', //评论内容

    showPro: true,
    baikeInfo: '',
    product: '',
    comments: [], //评论列表
    showCom: false
  },

  // 商品详情
  toDetail() {
    var id = this.data.baikeInfo.productId;
    wx.navigateTo({
      url: '/mainPackage/pages/productDetail/productDetail?id=' + id,
    })
  },

  // 发表评论
  sendHandle() {
    var text = this.data.text;
    if (text == '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      })
      return
    }


    if (util.filter_method(text).split("/")[1] == 1) {
      wx.showToast({
        title: '当前评论涉及到敏感词汇,请重新编辑',
        icon: 'none'
      })
      return
    }

    console.log(text);

    var url = app.globalData.url + "ScBaiKe/addTopicComment",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        topicId: that.data.baikeInfo.baikeId,
        userId: app.globalData.userId,
        commentDetail: text,
        parentId: 0
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.hideLoading()
          wx.showToast({
            title: '评论成功',
            icon: 'none'
          })
          that.setData({
            text: ''
          })
          that.getVideoDetail()
          console.log(res);
        }
      },
      err => {
        console.log(err);
      })





  },

  // 关闭弹窗
  onClose() {
    this.setData({
      showCom: false
    })

  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })

    console.log(this.data);
  },


  //   点赞  收藏

  conHandle(e) {
    console.log(e);
    var flag = e.currentTarget.dataset.flag;


    if (flag == 'like') {
      this.priseOrcollectHandle(0, this.data.isLike ? 0 : 1)
    } else if (flag == 'collection') {
      this.priseOrcollectHandle(1, this.data.isCollection ? 0 : 1)
    } else if (flag == 'comment') {
      this.setData({
        showCom: true
      })
    } else if (flag == 'share') {

      this.onShareAppMessage()

    }
  },

  // 点赞 收藏
  priseOrcollectHandle(type, status) {
    var url = app.globalData.url + "ScBaiKe/addCollectOrPrise",
      isLike = this.data.isLike,
      isCollection = this.data.isCollection,
      baikeInfo = this.data.baikeInfo,
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        productId: that.data.baikeInfo.baikeId,
        userId: app.globalData.userId,
        flag: that.data.baikeInfo.baikeType == 0 ? 1 : 2,
        type,
        status
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.hideLoading()
          if (type == 0) {
            isLike = !isLike
            wx.showToast({
              title: isLike ? '已点赞' : '取消点赞',
              icon: 'none'
            })
            if (isLike) {
              baikeInfo.priseNum += 1
            } else {
              baikeInfo.priseNum -= 1
            }
          } else if (type == 1) {
            isCollection = !isCollection
            wx.showToast({
              title: isCollection ? '已收藏' : '取消收藏',
              icon: 'none'
            })
            if (isCollection) {
              baikeInfo.collectionNum += 1
            } else {
              baikeInfo.collectionNum -= 1
            }
          }

          that.setData({
            isLike,
            isCollection,
            baikeInfo
          })

          console.log(res);
        }
      },
      err => {
        console.log(err);
      })
  },



  // 关闭商品弹窗
  closeHandle() {
    this.setData({
      showPro: false
    })
  },

  getVideoDetail() {
    var url = app.globalData.url + "ScBaiKe/listBaiKeComment",
      isLike = false,
      isCollection = false,
      comments = [],
      id = this.data.baikeInfo.baiKeId,
      that = this;
    wx.showLoading({
      title: '',
    })
    console.log(that.data.baikeInfo);
    console.log(that.data.baikeInfo.baiKeId);
    app.wxRequest(url, {
        baiKeId: that.data.baikeInfo.baikeId || id,
        // baiKeId: id,
        productId: that.data.baikeInfo.productId || 0,
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.hideLoading()
          isLike = res.collect.some(item => {
            return item.collectFlag == 0
          })
          isCollection = res.collect.some(item => {
            return item.collectFlag == 1
          })

          comments = res.comments

          console.log(comments);
          comments.forEach(item => {
            item.userImg = item.userImg || '/img/avatar.png'
          })
          that.setData({
            product: res.product,
            isLike,
            isCollection,
            comments
          })


          console.log(res);
        }
      },
      err => {
        console.log(err);
      })

  },

  // 图片加载失败
  errorHandle(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index,
      comments = this.data.comments;
    comments[index].userImg = '/img/avatar.png'
    this.setData({
      comments
    })
  },

  checkUserInfo() {
    var isLogin = wx.getStorageSync("isXiuZhengLogin"),
      that = this;
    if (!isLogin) {
      app.getInfo().then(function (res) {
        console.log(res);
        if (res.data.user.userName == '') {
          wx.reLaunch({
            url: '/userPackage/pages/userInfo/userInfo',
          })
        }
        app.globalData.user = res.data.user
        app.globalData.openId = res.data.user.openId
        app.globalData.userId = res.data.user.userId
        app.globalData.userType = res.data.user.userType
        app.globalData.usedPoints = res.data.user.usedPoints
        app.globalData.userScore = res.data.user.userScore
        app.globalData.diamondNum = res.data.user.diamondNum
        app.globalData.userBalance = res.data.user.userBalance
        wx.setStorageSync('isXiuZhengLogin', true)
        wx.setStorageSync('user', JSON.stringify(res.data.user))

        that.setData({
          userScore: app.globalData.userScore,
          userBalance: app.globalData.userBalance,
          diamondNum: app.globalData.diamondNum,
          usedPoints: app.globalData.usedPoints,
          user: app.globalData.user
        })
        if (!wx.getStorageSync('nextLevelScore')) {
          that.getVip(app.globalData.user.vipLevel)
        }
      })
    } else {
      if (!app.globalData.user.userName) {
        wx.reLaunch({
          url: '/userPackage/pages/userInfo/userInfo',
        })
      }
    }

  },
  getVip(level) {
    console.log(level);
    var url = app.globalData.url + "scVip/getVipLevelList";
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        companyId: app.globalData.companyId
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.setStorageSync('nextLevelScore', JSON.stringify(res.data))
          //最高等级
          // if(level==res.data[res.data.length-1].vipLevelNumber){
          //   wx.setStorageSync('nextLevelScore', res.data[res.data.length-1])
          // }else{
          //   level +=1;
          //   res.data.forEach(item => {
          //     if (item.vipLevelNumber == level) {
          //       wx.setStorageSync('nextLevelScore', item);
          //       break;
          //     }
          //   })
          // }

          wx.hideLoading()
        }
      },
      err => {
        console.log(err);
      })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    var isLogin = wx.getStorageSync("isXiuZhengLogin");

    // 如果没有用户信息，获取用户信息
    if (!isLogin) {
      this.checkUserInfo()
    }
    this.setData({
      baikeInfo: JSON.parse(options.obj),
      showPro: JSON.parse(options.obj).productId ? true : false
    })
    console.log(this.data.baikeInfo);
    this.getVideoDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.checkUserInfo()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var baikeInfo = this.data.baikeInfo;
    let myObj = {
      title: baikeInfo.baikeTitle,
      path: "/contentPackage/pages/baikeDetail/baikeDetail?obj=" + JSON.stringify(baikeInfo),
      imageUrl: baikeInfo.baikeImg
    }
    return myObj;
  },
  onShareTimeline() {
    var baikeInfo = this.data.baikeInfo;
    let myObj = {
      title: baikeInfo.baikeTitle,
      path: "/contentPackage/pages/baikeDetail/baikeDetail?obj=" + JSON.stringify(baikeInfo),
      imageUrl: baikeInfo.baikeImg
    }
    return myObj;
  },
})